package com.keyinong.caizhengting.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.ImageUtils;
import com.keyinong.caizhengting.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class XiaotieshidetialActivity extends Activity {

    private TextView tv_tital;
    private TextView tv_time;
    private TextView tv_content;
    private ImageView iv_pic;
    private ImageButton ib_back;
    private HttpTools httptools = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_xiaotieshidetial);
        tv_tital = (TextView) findViewById(R.id.tv_tital);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_content = (TextView) findViewById(R.id.tv_content);
        iv_pic = (ImageView) findViewById(R.id.iv_pic);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        ib_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Detail();
    }

    private void Detail() {
        if (NetWorkUtil.isNetwork(getApplicationContext()) == false) {
            Toast.makeText(getApplicationContext(), "当前网络不可用", Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        String id = getIntent().getStringExtra("id");
        Log.i("info", "id---" + id);
        if (httptools == null) {
            httptools = new HttpTools(XiaotieshidetialActivity.this);
        }
        httptools.getArticleDetail(token, id, new JsonXiaotieshidetail());
    }

    public class JsonXiaotieshidetail extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                Log.i("info", "小贴士" + response.toString());
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String brief = object.getString("brief");
                    String pic = object.getString("pic");
                    String content = object.getString("content");
                    String show_time = object.getString("show_time");
                    tv_tital.setText(brief);
                    tv_time.setText(show_time);
                    tv_content.setText(content);
                    ImageUtils.displayImage(XiaotieshidetialActivity.this, pic.replace("w/60", "w/120"), iv_pic, R.drawable.wupian1);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
